package com.dycong.common.threadUtil;

import lombok.experimental.Delegate;

import javax.annotation.PreDestroy;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * Created by Duke on 2017/1/20.
 */
public class ScheduleThreadService {

    public static final int DEFAULT_SIZE = 10;

    @Delegate
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(DEFAULT_SIZE);


    @PreDestroy
    public void destroy() {
        scheduledExecutorService.shutdown();
    }

    /*TODO 建立service......*/
    public static Runnable function2Runnable(Function<Integer, ?>function,Integer comparable){
        final Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                function.apply( comparable);
                System.out.println(Thread.currentThread().getName() + "---testFuture-Runnable");
            }
        };
        return myRunnable;
    }

    public void runningSchedule(){
        Function<Integer, ?> function = (x) -> {
            System.out.println(x*2*2*2*2*2*2*2*2*2*2*2*2>>6);
            return null;
        };
        Runnable runnable=function2Runnable(function,256);
        scheduledExecutorService.scheduleWithFixedDelay(runnable, 1000 * 1, 1000 * 1, TimeUnit.MILLISECONDS);

    }

    public static void main(String[] args) {
        ScheduleThreadService scheduleThreadService=new ScheduleThreadService();
        scheduleThreadService.runningSchedule();
    }


}
